package com.jiangaifeng.blog.service.impl;

import java.util.List;

import com.jiangaifeng.blog.domain.BlogPost;
import com.jiangaifeng.common.utils.DateUtils;
import com.jiangaifeng.common.utils.file.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jiangaifeng.blog.mapper.BlogPhotoMapper;
import com.jiangaifeng.blog.domain.BlogPhoto;
import com.jiangaifeng.blog.service.IBlogPhotoService;

/**
 * 照片Service业务层处理
 * 
 * @author jiangaifeng
 * @date 2025-02-13
 */
@Service
public class BlogPhotoServiceImpl implements IBlogPhotoService 
{
    @Autowired
    private BlogPhotoMapper blogPhotoMapper;

    /**
     * 查询照片
     * 
     * @param id 照片主键
     * @return 照片
     */
    @Override
    public BlogPhoto selectBlogPhotoById(Long id)
    {
        return blogPhotoMapper.selectBlogPhotoById(id);
    }

    /**
     * 查询照片列表
     * 
     * @param blogPhoto 照片
     * @return 照片
     */
    @Override
    public List<BlogPhoto> selectBlogPhotoList(BlogPhoto blogPhoto)
    {
        return blogPhotoMapper.selectBlogPhotoList(blogPhoto);
    }

    /**
     * 新增照片
     * 
     * @param blogPhoto 照片
     * @return 结果
     */
    @Override
    public int insertBlogPhoto(BlogPhoto blogPhoto)
    {
        blogPhoto.setCreateTime(DateUtils.getNowDate());
        blogPhoto.setUpdateTime(DateUtils.getNowDate());
        return blogPhotoMapper.insertBlogPhoto(blogPhoto);
    }

    /**
     * 修改照片
     * 
     * @param blogPhoto 照片
     * @return 结果
     */
    @Override
    public int updateBlogPhoto(BlogPhoto blogPhoto)
    {
        blogPhoto.setUpdateTime(DateUtils.getNowDate());
        return blogPhotoMapper.updateBlogPhoto(blogPhoto);
    }

    /**
     * 批量删除照片
     * 
     * @param ids 需要删除的照片主键
     * @return 结果
     */
    @Override
    public int deleteBlogPhotoByIds(Long[] ids)
    {
        //删除minio中的照片
        for( Long id : ids ){
            BlogPhoto photo = blogPhotoMapper.selectBlogPhotoById( id );
            FileUploadUtils.deleteMinioEx( photo.getUrl1(),"photo" );
            FileUploadUtils.deleteMinioEx( photo.getUrl2(),"photo" );
        }
        return blogPhotoMapper.deleteBlogPhotoByIds(ids);
    }

    /**
     * 删除照片信息
     * 
     * @param id 照片主键
     * @return 结果
     */
    @Override
    public int deleteBlogPhotoById(Long id)
    {
        return blogPhotoMapper.deleteBlogPhotoById(id);
    }
}
